package kr.poturns.blink.internal;

import java.util.ArrayList;

import android.content.Context;
import android.content.pm.FeatureInfo;
import android.content.pm.PackageManager;

/**
 * 
 * @author Yeonho.Kim
 * @since 2014.07.17
 *
 */
public class DeviceAnalyzer {
	
	// *** CONSTANT DECLARATION *** //
	public enum Ego {
		HEART,
		PERIPHERALS
	}
	
	
	// *** STATIC DECLARATION *** //
	private static DeviceAnalyzer sInstance = null;
	
	/**
	 * 
	 * @param context
	 * @return
	 */
	public static DeviceAnalyzer getInstance(Context context) {
		if (sInstance == null && context != null)
			sInstance = new DeviceAnalyzer(context);
		return sInstance;
	}

	public static int getEgoPoint() {
		return (sInstance == null)? 0 : sInstance.mEgoPoint;
	}
	
	
	// *** FIELD DECLARATION *** //
	private final Context ANALYZER_CONTEXT;
	private final ArrayList<String> ANALYSIS_ARRAY;
	private int mEgoPoint;
	
	public boolean hasBluetoothLE;
	
	public DeviceAnalyzer(Context context) {
		this.ANALYZER_CONTEXT = context;
		this.ANALYSIS_ARRAY = new ArrayList<String>();
		
		analyze();
	}
	
	private void analyze() {
		PackageManager mPackageManager = ANALYZER_CONTEXT.getPackageManager();
		
		ANALYSIS_ARRAY.clear();
		for (FeatureInfo feature : mPackageManager.getSystemAvailableFeatures()) 
			ANALYSIS_ARRAY.add(feature.name);
		
		mEgoPoint = 0;
		
		// - Check Bluetooth LE Support.
		hasBluetoothLE = ANALYSIS_ARRAY.contains(PackageManager.FEATURE_BLUETOOTH_LE);
		
		// - Check Telephony Support.
		if (ANALYSIS_ARRAY.contains(PackageManager.FEATURE_TELEPHONY))
			mEgoPoint |= 4096;
			
		// - Check Wifi Support.
		if (ANALYSIS_ARRAY.contains(PackageManager.FEATURE_WIFI))
			mEgoPoint |= 1024;
			
		// - Check Wifi-Direct Support.
		if (ANALYSIS_ARRAY.contains(PackageManager.FEATURE_WIFI_DIRECT))
			mEgoPoint |= 2048;
		
		// - Check Bluetooth Support.
		if (!ANALYSIS_ARRAY.contains(PackageManager.FEATURE_BLUETOOTH))
			mEgoPoint &= 0;
	}
	
	public int apply() {
		
		return mEgoPoint;
	}
	
	/**
	 * 
	 * @param feature : {@link PackageManager}의 Feature 상수.
	 * @return 
	 */
	public boolean isAvailableFeature(String feature) {
		return ANALYSIS_ARRAY.contains(feature);
	}
}
